package mx.kolobok.noteeditor.ui.keymap.actions;

import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.Caret;
import javax.swing.text.DefaultEditorKit;
import javax.swing.text.JTextComponent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

/**
 * User:  Nikita Belenkiy
 * Date: 20.05.11
 * Time: 4:24
 */
public class CutAction extends DefaultEditorKit.CutAction {

    public CutAction(@NotNull JTextComponent editorPane) {
        putValue(Action.NAME, "Cut");
        putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK));

        final Caret caret = editorPane.getCaret();
        caret.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                setEnabled(caret.getDot() != caret.getMark());
            }
        });
    }


}
